@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

:: 设置颜色变量
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
set "RED=!DEL![91m"
set "GREEN=!DEL![92m"
set "YELLOW=!DEL![93m"
set "BLUE=!DEL![94m"
set "RESET=!DEL![0m"

echo %BLUE%正在启动前后端服务...%RESET%
echo.

:: 检查Python环境
echo %YELLOW%[1/4] 检查Python环境...%RESET%
python --version >nul 2>&1
if errorlevel 1 (
  echo %RED%未检测到Python环境，请先安装Python%RESET%
  pause
  exit /b 1
)

:: 检查后端依赖
echo %YELLOW%[2/4] 检查后端依赖...%RESET%
cd backend
if not exist "run.py" (
  echo %RED%未找到run.py文件%RESET%
  pause
  exit /b 1
)
cd ..

:: 启动后端服务
echo %YELLOW%[3/4] 启动后端服务...%RESET%
cd backend
start "后端服务" cmd /k "echo %GREEN%后端服务启动中...%RESET% & python run.py || (echo %RED%后端服务启动失败!%RESET% & echo 可能原因: & echo 1. Python依赖未安装 & echo 2. 端口被占用 & echo 3. 配置文件错误 & pause)"
cd ..

:: 检查前端依赖
echo %YELLOW%[3/4] 检查前端依赖...%RESET%
cd frontend
if not exist "node_modules" (
  echo %RED%前端node_modules不存在，请先运行npm install%RESET%
  pause
  exit /b 1
)
cd ..

:: 启动前端服务
echo %YELLOW%[4/4] 启动前端服务...%RESET%
cd frontend
start "前端服务" cmd /k "echo %GREEN%前端服务启动中...%RESET% & npm run dev || (echo %RED%前端服务启动失败!%RESET% & echo 可能原因: & echo 1. 端口被占用 & echo 2. 依赖未安装 & echo 3. 配置文件错误 & pause)"
cd ..

echo.
echo %GREEN%前后端服务启动命令已执行，请查看新打开的终端窗口...%RESET%
echo 注意: 如果服务启动失败，窗口会保持打开显示错误信息
pause